<?php


namespace app\common\service\companyIndustry\industrymodel;

use app\common\service\CompanyService;
use app\common\service\ControlService;
use app\common\service\{
    companyIndustry\tsinghua\TsinghuaEmissionService, EmissionService, InletFlueGasService, FlugGasService
};

/**
 * 清华排口排放量 转 设施 运行浓度和运行效率的 模型
 */
class TsinghuaDrainEmissionToFacilityParam
{
    /**
     * 获取方案运行浓度和运行效率 - 根据设施ID 和 排口ID
     * @author hongwei 2019-07-26
     * @param int $facilityId 设施ID
     * @param string $planNumber 预案编号
     * @param string $tsinghuaName 清华方案名称
     * @param $type
     * @return mixed
     * @throws \MongoDB\Driver\Exception\Exception
     */
    public function getPlanParamByFacilityId($facilityId, $planNumber, $tsinghuaName, $type)
    {
        //step1:根据设施ID获取排口ID
        $company = new CompanyService();
        $drainId = $company->getDrainIdByFacilityId($facilityId);

        //step1:获取设施数组

        $facilityIdArray = $company->getFacilityArrayByDrainId($drainId, $type);

        //step2根据设施ID获取公司设施ID位置
        $facilityPos = array_search($facilityId, $facilityIdArray);

        //step3:获取运行浓度和效率
        $data                    = $this->getOperationConcentrationAndEfficiencyProjectValue($planNumber, $tsinghuaName, $facilityIdArray, $type);
        $result['efficiency']    = $data['yita'];
        $result['concentration'] = $data['rouOut'][$facilityPos];
        return $result;
    }

    /**
     * 李博士的模型 - 获取运行浓度和运行效率的方案值
     * @author yangyi
     * @param $planNumber '预案编号'
     * @param $tsinghuaName '预案名称应的名称'
     * @param array $facilityId '设施编号'
     * @param $type 'SO2 NO2'
     * @return mixed
     * @throws \MongoDB\Driver\Exception\Exception
     * @author yy by 2019/7/25
     */
    public function getOperationConcentrationAndEfficiencyProjectValue($planNumber, $tsinghuaName, $facilityId = [], $type)
    {
        $emissionService     = new TsinghuaEmissionService();
        $inletFlueGasService = new InletFlueGasService();
        $flugGasService      = new FlugGasService();
        $control             = new ControlService();
        $startTime           = $control->getBeforeStartTime($planNumber);
        $endTime             = $control->getBeforeEndTime($planNumber);
        $param_end_time      = $endTime . ' 23:59:59';
        // 排口同类治理设施数量
        $M = count($facilityId); //晋能热电脱硫2，脱硝4
        // 设定的排口污染物排放量的小时均值 脱硫两个设施
        $mOutHour = $emissionService->getTsinghuaSingleCompanyDrainEmission($type, 1, $planNumber, 1, $tsinghuaName);
        for ($i = 0; $i < $M; $i++) {
            // 排口同类治理设施入口烟气量数组
            $Qin[$i] = $inletFlueGasService->getCompanyFacilityInletFlueGasVolumeDay($facilityId[$i], $planNumber, $type, 'minute');
            // 排口同类治理设施出口烟气量数组
            $Qout[$i] = $flugGasService->monitorSulfurExitSmokeMinuteAvg($startTime, $endTime, $facilityId[$i], $type);
            // 排口同类治理设施入口污染物浓度数组
            $rouIn[$i] = $inletFlueGasService->getCompanyFacilityInletConcentration($facilityId[$i], $planNumber, $type);
        }
        if ($type === 'SO2') {
            $param_qin   = '&qin=' . $Qin[0] . '&qin=' . $Qin[1];
            $param_qout  = '&qOut=' . $Qout[0] . '&qOut=' . $Qout[1];
            $param_rouIn = '&rouIn=' . $rouIn[0] . '&rouIn=' . $rouIn[1];
        } else {
            $param_qin   = '&qin=' . $Qin[0] . '&qin=' . $Qin[1] . '&qin=' . $Qin[2] . '&qin=' . $Qin[3];
            $param_qout  = '&qOut=' . $Qout[0] . '&qOut=' . $Qout[1] . '&qOut=' . $Qout[2] . '&qOut=' . $Qout[3];
            $param_rouIn = '&rouIn=' . $rouIn[0] . '&rouIn=' . $rouIn[1] . '&rouIn=' . $rouIn[2] . '&rouIn=' . $rouIn[3];
        }
        $param  = 'http://39.96.33.44:8081/renren-api/jnrdgk/disassemble?mOutHour=' . $mOutHour[1]['tsinghua_emission'] * 1000000 . $param_qin . $param_qout . $param_rouIn . '&m=' . $M;
        $result = $this->curl_get_https($param);
        return $result;
    }

    /**
     * @param $url
     * @return array
     * @author yy by 2019/7/25
     */
    public function curl_get_https($url)
    {
        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);  // 从证书中检查SSL加密算法是否存在
        $tmpInfo = json_decode(curl_exec($curl), true);
        //关闭URL请求
        curl_close($curl);
        return ['yita' => $tmpInfo['yita'], 'rouOut' => $tmpInfo['rouOut']];
    }
}